package plate

import (
	"container/list"
	"time"

	"gitee.com/wulalade/hwpack/lpr/aiApiT"
)

//保存数据到队列里面
func Queue(user_id uint, carRsps []aiApiT.Car, isEmpty bool) {
	var (
		node      Node_t
		rsps_len  int //carRsps的长度
		queue_len int //队列的长度
	)

	node, _ = get(user_id)

	rsps_len = len(carRsps)
	for k1 := 0; k1 < maxLogic; k1++ {
		if rsps_len == 0 {
			node.Logic[k1].Used = false
			continue
		}
		for k2, carRsp := range carRsps {
			if uint(k1) == carRsp.LogicNum {
				//插入缓存队列
				node.Logic[k1].Used = true
				//初始化队列
				if node.Logic[k1].Queue == nil {
					node.Logic[k1].Queue = list.New()
				}
				var res CarResult_t
				res.IsEmpty = isEmpty
				res.Car = carRsp
				queue_len = node.Logic[k1].Queue.Len()
				//小于直接插入
				if queue_len >= maxCache {
					//删除队列的数据,使添加完后,还保证缓存值不变
					for i := 0; i < (queue_len - maxCache + 1); i++ {
						e := node.Logic[k1].Queue.Front()
						if e != nil {
							node.Logic[k1].Queue.Remove(e)
						}
					}
				}
				//校验前两位是否正常
				res = carResultAline(res)
				node.Logic[k1].Queue.PushBack(res)
				break
			}
			if k2+1 == rsps_len {
				node.Logic[k1].Used = false
			}
		}
	}
	time_now := time.Now()
	//遍历每个队列,获取值
	for i := 0; i < maxLogic; i++ {
		if !node.Logic[i].Used {
			continue
		}
		node.Logic[i].getCarResult(time_now)
	}

	//保存回去
	set(user_id, node)
}

//更新数据的时间
func UplogicTime(user_id uint, logicNum uint, localName string, timeValue time.Time, plateNO, plateColor, carType string) {
	var (
		node Node_t
	)
	if logicNum >= maxLogic {
		return
	}

	//从map获取值
	node, _ = get(user_id)
	node.Logic[logicNum].Now_CarResult.Car.LogicNum = logicNum
	node.Logic[logicNum].Now_CarResult.Car.LocalName = localName
	node.Logic[logicNum].Now_CarResult.AppearTime = timeValue
	//add by xqm 20210930
	node.Logic[logicNum].Now_CarResult.Car.PlateNO = plateNO
	node.Logic[logicNum].Now_CarResult.Car.PlateColor = plateColor
	node.Logic[logicNum].Now_CarResult.Car.CarType = carType
	//保存回去
	set(user_id, node)
}

//获取值
func GetCarResult(user_id uint, logicNum uint, size int) []CarResult_t {
	carResults := make([]CarResult_t, 0)
	node, _ := get(user_id)
	if logicNum >= maxLogic {
		//设置获取的限制
		if size > maxLogic {
			size = maxLogic
		}
		for i := 0; i < size; i++ {
			carResults = append(carResults, node.Logic[i].Now_CarResult)
		}
	} else {
		carResults = append(carResults, node.Logic[logicNum].Now_CarResult)
	}
	return carResults
}

//只获取一个识别结果
func GetCarResultOnlyOne(user_id uint, logicNum uint) CarResult_t {
	var carResult CarResult_t
	node, _ := get(user_id)
	if logicNum >= maxLogic {
		carResult = node.Logic[0].Now_CarResult
	} else {
		carResult = node.Logic[logicNum].Now_CarResult
	}
	return carResult
}

func GetCarResultQueueOnlyOne(user_id uint, logicNum uint) (CarResult_t, []CarResult_t) {
	var carResult CarResult_t
	var carResults []CarResult_t
	node, _ := get(user_id)
	if logicNum >= maxLogic {
		carResult = node.Logic[0].Now_CarResult
		carResults = append(carResults, node.Logic[0].getAllwhitNoEmpty()...)
	} else {
		carResult = node.Logic[logicNum].Now_CarResult
		carResults = append(carResults, node.Logic[logicNum].getAllwhitNoEmpty()...)
	}
	return carResult, carResults
}

//设置默认的省份,例如 闽
func SetProvince(province string) {
	handler.defProvince = province
}

//设置默认的省份的字母,例如 D
func SetCity(city string) {
	handler.defCity = city
}
